/*
HOSPITAL CORPORATIZATION

THIS DO FILE ESTIMATES RISK ADJUSTED MORTALITY ESTIMATES FOR ALL HOSPITALS IN 2012-13 OVER NONDEF PATIENTS ADMITTED THROUGH THE ED. EXPORT AND USE FOR MOTIVATIONAL REGS.


DEC 24, 2023;
PREPARE FOR REPLICATION PACKAGE;
*/

clear all
set more off

global datapath "<folder containing project data files>"

*define vector of patient controls;
local patx male age_70_74 age_75_79 age_80_84 age_85_89 age_m90 histelsum hist1y drgwt

*set threshold volume to retain hospital in the sample;
local thres 20

use "$datapath/desc_enr_hist_readm_sample", clear

di "drop cases at hospitals not matched with AHAID"
drop if ahaid==""

di "retain patients enrolled in ffs for a year prior to admission"
keep if enr==1

di "keep only those admitted through ed for non-deferrable admission"
keep if ed==1 & i_nondef==1

di "retain patients without prior admission in the last 90 days"
keep if hist90d==0

di "drop patients <age 65 at admission"
drop if ageatadmsn<65

gen mort90d = (final_deathdt - admsn_dt)<=90

tempfile base
save `base', replace
	
*Only retain hospitals with min. 25 cases;
bys ahaid: gen hospobs = _N

preserve 
	gen one=1
	gcollapse (sum) tot=one, by(ahaid)
	summ tot, d
	count if tot<`thres'
restore

drop if hospobs<`thres'

******************************
*Summary stats; 

gen one=1
gen mort90d_2012 = mort90d if year==2012
gen mort90d_2013 = mort90d if year==2013
gen readm90d_2012 = readm90d if year==2012
gen readm90d_2013 = readm90d if year==2013
gcollapse (sum) tot=one (mean) mort90d* readm90d* male ageatadmsn age_* histelsum hist1y, by(ahaid hrrcode)
	
tempfile hosplevel
save `hosplevel', replace

order ahaid tot readm90d readm90d_2012 readm90d_2013 mort90d mort90d_2012 mort90d_2013  
export excel using "$datapath/utility/hosp_metrics.xlsx", firstrow(var) replace

*END CODE;

